<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Music;
use App\Models\MusicParagraph;
use App\Models\MusicParagraphConfig;
use App\Models\MusicTypeMenu;
use Illuminate\Http\Request;

class MusicParagraphConfigController extends Controller
{

    public function save(Request $request)
    {
        $id = $request->post("id");

        // 删除所有的段落配置
        // 添加新的段落配置

        MusicParagraphConfig::where("music_paragraph_id",$id)->delete();

        $musicParagraphModel = MusicParagraph::find($id);

        $musicModel = Music::find($musicParagraphModel->music_id);

        $musicTypeMenuModels = MusicTypeMenu::where("music_type_id", $musicModel->music_type_id)
            ->get();
        $arrTmp = [];
        foreach ($musicTypeMenuModels as $v){
            $arrTmp[] = [
                "music_paragraph_id" => $id,
                "music_type_menu_code" => $v->music_type_menu_code,
                "music_paragraph_file" => $request->post($v->music_type_menu_code),
            ];
        }
//        dd( $arrTmp );
        MusicParagraphConfig::upsert($arrTmp, [
            ["music_paragraph_id","music_type_menu_code"],
            ["music_paragraph_file"]
        ]);


        return redirect()->route("adminMusicParagraphIndex", ["musicId" => $musicModel->id]);

    }



    public function edit(Request $request)
    {
        // 段落 ID
        $id = $request->input("id");

        $musicParagraphModel = MusicParagraph::find($id);

        $musicParagraphConfigModels = MusicParagraphConfig::where("music_paragraph_id", $id)
            ->pluck("music_paragraph_file", "music_type_menu_code");


        $musicModel = Music::find($musicParagraphModel->music_id);

        $musicTypeMenuModels = MusicTypeMenu::where("music_type_id", $musicModel->music_type_id)
            ->get();
//        dump($musicParagraphModel);
//        dump($musicModel);
//        dump($musicTypeMenuModels);
        $pageName = $musicModel->music_name . " " . $musicParagraphModel->music_paragraph_name . " 段落配置";

        return view("admin.musicParagraphConfig.edit", [
            "musicTypeMenuModels" => $musicTypeMenuModels,
            "musicParagraphModel" => $musicParagraphModel,
            "musicParagraphConfigModels" => $musicParagraphConfigModels,
            "musicModel" => $musicModel,
            "pageName" => $pageName,

        ]);
    }

}
